package com.hqyj.javaSpringCloudAccount.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.entity.account.Resource;
import com.hqyj.entity.common.Result;
import com.hqyj.entity.common.SearchBean;
import com.hqyj.javaSpringCloudAccount.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/account/")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 根据 id 删除资源 接口
     * 127.0.0.1:8080/api/resource/{id} ---- delete
     */
    @DeleteMapping("/resource/{id}")
    public Result<Object> deleteResourceById(@PathVariable int id){
        return resourceService.deleteResourceById(id);
    }

    /**
     * 更新资源信息 接口
     * 127.0.0.1:8080/api/resource ---- put
     * {"id":6,"permit":"/account/resources"}
     */
    @PutMapping(value = "/resource",consumes = "application/json")
    public Result<Resource> updateResource(@RequestBody Resource resource){
        return resourceService.updateResource(resource);
    }

    /**
     * 添加资源 resource 接口
     * 127.0.0.1:8080/api/resource ---- post
     * {"permit":"/account/resource"}
     */
    @PostMapping(value = "/resource",consumes = "application/json")
    public Result<Resource> insertResource(@RequestBody Resource resource){
        return resourceService.insertResource(resource);
    }

    /**
     * 根据 id 查询资源接口
     * 127.0.0.1:8080/api/resource/1 ---- get
     */
    @GetMapping("/resource/{id}")
    public Resource getResourceById(@PathVariable int id){
        return resourceService.getResourceById(id);
    }

    /**
     * resources 多条件分页查询 接口
     * 127.0.0.1:8080/api/resources ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"id","direction":"asc","keyword":""}
     */
    @PostMapping(value = "/resources", consumes = "application/json")
    public PageInfo<Resource> getResourceBySearchBean(@RequestBody SearchBean searchBean){
        return resourceService.getResourceBySearchBean(searchBean);
    }
}
